package cs4280.ass2.util;

import org.apache.commons.lang.StringUtils;

import org.apache.commons.validator.EmailValidator;

import cs4280.ass2.entity.User;
import cs4280.ass2.exception.ValidateException;

public class UserUtils {
	
	public static void validate(User user) throws ValidateException {
		
		// Login name
		if (StringUtils.isBlank(user.getLogin())) {
			throw new ValidateException("message.error.validate.user.login.empty");
		} else {
			// ensure it is valid email address.
			if (!EmailValidator.getInstance().isValid(user.getLogin())) {
				throw new ValidateException("message.error.validate.user.login.notValid");
			}
		}
		
		// Password
		if (StringUtils.isBlank(user.getPassword())) {
			throw new ValidateException("message.error.validate.user.password.empty");
		} else {
			if (user.getPassword().length() < 8){
				throw new ValidateException("message.error.validate.user.password.tooShort");
			}
		}
		
		// First Name
		if (StringUtils.isBlank(user.getFirstName())) {
			throw new ValidateException("message.error.validate.user.firstname.empty");
		}
		
		// Last Name
		if (StringUtils.isBlank(user.getLastName())) {
			throw new ValidateException("message.error.validate.user.lastname.empty");
		}
		
		// User Type
		if (!isValidUserType(user.getType())) {
			throw new ValidateException("message.error.validate.user.type.invalid");
		}
		
		// User Status
		if (!isValidUserStatus(user.getAppUserStatus())) {
			throw new ValidateException("message.error.validate.user.status.invalid");
		}
	}
	
	public static boolean isValidUserType(String userType) {
		return (StringUtils.equals(userType, User.USER_TYPE_MANAGER) ||
				StringUtils.equals(userType, User.USER_TYPE_OFFCIER) ||
				StringUtils.equals(userType, User.USER_TYPE_MEMBER)   );
	}
	public static boolean isValidUserStatus(String userStatus) {
		return (StringUtils.equals(userStatus, User.APP_USER_ACTIVE) ||
				StringUtils.equals(userStatus, User.APP_USER_DELETED)   );
	}
	
	public static boolean isDeleted(User user) {
		return (StringUtils.equals(user.getAppUserStatus(), User.APP_USER_DELETED));
	}
	
}
